import { Component,ViewChild } from '@angular/core';
import { IonicPage, NavController, NavParams,Slides,ViewController } from 'ionic-angular';
import {HousesService} from '../../providers/houses.service';
import { Storage } from '@ionic/storage';
import {EvaluationPage} from '../evaluation/evaluation';

/**
 * Generated class for the HouseDetailPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */
declare var $:any;


@IonicPage()
@Component({
  selector: 'page-house-detail',
  templateUrl: 'house-detail.html',
  providers:[HousesService]

})
export class HouseDetailPage {
  @ViewChild(Slides) slides: Slides;

  // @oninput(this.houseId)
  imgs = ['1.jpg','2.jpg','3.jpg'];
  users;
  usersLength:number = 0;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private hoSer:HousesService,
    private storage:Storage,

  ) { }

  shou:number=0;
  kan:number=0;
  house:any;
  userId;


  ionViewDidLoad() {
    this.house=this.navParams.get('house');
    let that = this;
    // console.log(that.storage.get('user'))


    that.storage.ready().then(() =>{
      that.storage.get('user').then((user) =>{
        // console.log(user);
        that.userId = user.userId;
        that.hoSer.isFocus({"houseId":that.house.houseId,"userId":that.userId},function (result) {
          // console.log(result.stateCode);
          if(result.stateCode == 33){
            that.shou=1;
            $('#shou').html('已收藏')
          }else{
            that.shou=0;
            $('#shou').html('收藏此房')
          }
        });
        that.hoSer.isAsk({"houseId":that.house.houseId,"userId":that.userId},function (result) {
          // alert(result.stateCode);
          if(result.stateCode == 43){
            $('#kan').html('预约看房中');
            that.kan=1
          }else{
            that.kan=0;
            $('#kan').html('我要看房')
          }
        });
      });

    })



    that.house=this.navParams.get('house');
    that.hoSer.getArrInfo({"houseId":that.house.houseId},function (result) {
      if(result && result.length){
        // console.log(result);
        that.users=result;
        that.usersLength = result.length;
        console.log(that.usersLength);
      }

    })

  }


  slideChanged(){
    // let currentIndex = this.slides.getActiveIndex();
    // console.log('Current index is', currentIndex);
    this.slides.startAutoplay();
  }

  back(){
    this.navCtrl.pop();
  }


  collect(){
    let that = this;
    if(that.shou==0){
      $('#shou').html('已收藏')
      that.shou=1;
      that.hoSer.focus({"houseId":that.house.houseId,"userId":that.userId},function (result) {
        // console.log(result.stateCode);
        if(result.stateCode == 21){
          console.log("收藏成功");

        }else{
          console.log("收藏失败");
        }
      });
    }else if(that.shou==1){
      $('#shou').html('收藏此房')
      that.shou=0;
      that.hoSer.unFocus({"houseId":that.house.houseId,"userId":that.userId},function (result) {
        // alert(result.stateCode);
        if(result.stateCode == 23){
          console.log("取消收藏");
        }else{
          console.log("取消失败");
        }
      });
    }
  }
  toEvaluation(){
    let that = this;
    that.house=this.navParams.get('house');
    that.hoSer.getArrInfo({"houseId":that.house.houseId},function (result) {
      if(result.length){
        console.log(result);
        that.users=result;
      }

    })
    that.navCtrl.push(EvaluationPage,{"users":that.users,"house":that.house});

  }
  kanfang(){
    let that = this;
    if(that.kan==0){
      $('#kan').html('预约看房中')
      that.kan=1;
      that.hoSer.askWatch({"houseId":that.house.houseId,"userId":that.userId},function (result) {
        // alert(result.stateCode);
        if(result.stateCode == 39){
          console.log("预约成功");

        }else{
          console.log("预约失败");
        }
      });
    }else if(that.kan==1){
      $('#kan').html('预约看房中')
    }
  }

}
